<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="40%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="所属图书馆" prop="library_id">
        <el-select
          v-model="form.library_id"
          placeholder="请选择图书馆"
          style="width: 100%"
          filterable
        >
          <el-option
            v-for="item in libraryOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="楼层编号" prop="floor_number">
        <el-input v-model="form.floor_number" placeholder="例如：1、2、B1" />
      </el-form-item>
      <el-form-item label="楼层描述" prop="description">
        <el-input
          v-model="form.description"
          type="textarea"
          :rows="4"
          placeholder="请输入楼层描述"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" style="width: 100%">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "FloorAdd",
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "新增楼层",
      isEdit: false,
      form: {
        library_id: undefined,
        floor_number: "",
        description: "",
        status: 1
      },
      rules: {
        library_id: [
          { required: true, message: "请选择所属图书馆", trigger: "change" }
        ],
        floor_number: [
          { required: true, message: "楼层编号不能为空", trigger: "blur" },
          { max: 20, message: "楼层编号不能超过20个字符", trigger: "blur" }
        ],
        description: [
          { max: 255, message: "楼层描述不能超过255个字符", trigger: "blur" }
        ],
        status: [
          { required: true, message: "请选择状态", trigger: "change" }
        ]
      },
      libraryOptions: [],
      statusOptions: [
        { label: "开放", value: 1 },
        { label: "关闭", value: 0 }
      ]
    };
  },
  methods: {
    open(row) {
      this.dialogVisible = true;
      this.getLibraryOptions();
      
      if (row) {
        this.isEdit = true;
        this.dialogTitle = "编辑楼层";
        this.form = { ...row };
        
        // 确保status是数字
        this.form.status = parseInt(this.form.status, 10);
      } else {
        this.isEdit = false;
        this.dialogTitle = "新增楼层";
        this.form = {
          library_id: undefined,
          floor_number: "",
          description: "",
          status: 1
        };
      }
      
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    
    getLibraryOptions() {
      http.libraries.list().then(response => {
        this.libraryOptions = response.data;
      });
    },
    
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              await http.libraryFloors.update(this.form.id, this.form);
              this.$message.success("更新成功");
            } else {
              await http.libraryFloors.create(null, this.form);
              this.$message.success("新增成功");
            }
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {
            console.error(error);
            if (error.response && error.response.data && error.response.data.message) {
              this.$message.error(error.response.data.message);
            } else {
              this.$message.error("操作失败，请检查输入是否正确");
            }
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
</style>
